package com.caiyi.financial.nirvana.sms.mapper;

import com.caiyi.financial.nirvana.sms.base.mapper.BaseMapper;
import com.caiyi.financial.nirvana.sms.entity.SmsGatewaySign;
import com.caiyi.financial.nirvana.sms.querybean.GatewaySignQueryBean;
import com.caiyi.financial.nirvana.sms.vo.SmsGatewaySignVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface SmsGatewaySignMapper extends BaseMapper<SmsGatewaySign> {

    @SelectProvider(type = GatewaySignSqlProvider.class, method = "selectGatewaySignListByParams")
    List<SmsGatewaySignVo> selectGatewaySignListByParams(GatewaySignQueryBean bean);

    @Update("update sms_gateway_sign set status=( case status when 1 then 2 else 1 end) where id=#{id}")
    int changeStatus(@Param("id") Integer id);

    class GatewaySignSqlProvider {
        public String selectGatewaySignListByParams(GatewaySignQueryBean bean) {
            return new SQL() {
                {
                    SELECT("s.id,g.name,s.sign,s.create_time as createTime,s.update_time as updateTime, s.access_number as accessNumber,s.status*1 as status");
                    FROM("sms_gateway_sign s");
                    LEFT_OUTER_JOIN("sms_gateway g on s.gateway_id=g.id");
                    if (!StringUtils.isEmpty(bean.getName())) {
                        WHERE("g.name LIKE concat('%',#{name},'%')");
                    }
                    if (!StringUtils.isEmpty(bean.getSign())) {
                        WHERE("s.sign LIKE concat('%',#{sign},'%')");
                    }
                    if (bean.getStartTime() != null) {
                        WHERE("s.create_time >= #{startTime}");
                    }
                    if (bean.getEndTime() != null) {
                        WHERE("s.create_time <= date_add(#{endTime}, interval 1 day)");
                    }
                    ORDER_BY("id desc");
                }
            }.toString();
        }
    }

}
